<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>
        a{text-decoration: none;font-size: 14px;text-align: center}
        a:link{color:#cccccc }
        a:hover{color: white}
        .center{width: 1200px;margin: 0 auto}
        .el-dropdown-link {
            cursor: pointer;
            color: #409EFF;
        }
        .el-icon-arrow-down {
            font-size: 12px;
        }
        .el-select .el-input {
            width: 130px;
        }
        .input-with-select .el-input-group__prepend {
            background-color: #fff;
        }
    </style>
</head>
<body>
<div id="app">
    <el-container>
        <my-header></my-header>
        <el-main style="background-image: url('imgs/login-background.png');height: 800px;
                background-size: cover;background-position: center;">
            <el-card style="width: 500px;margin: 0 auto">
                <el-form class="demo-ruleForm" :model="user" status-icon :rules="rules" ref="ruleForm"
                         label-width="80px" show-message="true" inline-message="true" label-position="left">
                    <el-form-item>
                        <h1 style="float: left;font-size: 28px">修改密码</h1>
                    </el-form-item>
                    <el-form-item
                            prop="email"
                            label="邮箱"
                            :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
                    >
                        <el-input v-model="user.email"></el-input>
                        <el-button type="primary" :disabled="isSend||isCountingDown" @click="sendCode">{{message}}</el-button>
                    </el-form-item>
                    <el-form-item label="验证码"  >
                        <el-input type="text" v-model="user.emailCode" autocomplete="off" ></el-input>
                        <el-button type="primary" :disabled="isRight" @click="compare">验证</el-button>
                    </el-form-item>
                    <el-form-item label="密码" prop="password" v-show="isRight">
                        <el-input type="password" v-model="user.password" autocomplete="off" ></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass" v-show="isRight">
                        <el-input type="password" v-model="user.checkPass" autocomplete="off" ></el-input>
                    </el-form-item>
                    <el-form-item v-show="isRight">
                        <el-button type="primary"  @click="submitForm('ruleForm')">修改</el-button>
                        <el-button type="primary" @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-main>
        <el-footer style="margin: 0;padding: 0">
           <my-footer></my-footer>
        </el-footer>

    </el-container>
</div>




<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<!--引入Axios-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script src="js/my-header.js"></script>
<script src="js/my-footer.js"></script>

<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 100);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.user.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                isRight:false,
                responseCode:'',
                isSend:false,
                phone: '',
                verificationCode: '',
                isCountingDown: false,
                countDownSeconds: 60,
                dynamicValidateForm: {
                    domains: [{
                        value: ''
                    }],
                    email: ''
                },
                ruleForm: {
                    password: '',
                    checkPass: '',
                    age: ''
                },
                user: {
                    password: '',
                    checkPass: '',
                    nickName:'',
                    email:"",
                    emailCode:""
                },

                rules: {
                    nickname:[{ validator: validatePass, trigger: 'blur' }],
                    password: [{ validator: validatePass, trigger: 'blur' }],
                    checkPass: [{ validator: validatePass2, trigger: 'blur' }],
                    age: [{ validator: checkAge, trigger: 'blur' }],
                    username:[{ validator: validatePass, trigger: 'blur' }]
                },
            }
        },
        computed: {
            message() {
                if (this.isSend) {
                    return '发送中...'
                }
                if (this.isCountingDown) {
                    return `${this.countDownSeconds}秒后重新获取`
                }
                return '获取验证码'
            }
        },

        methods: {
            compare(){
                if(v.responseCode!=v.user.emailCode){
                    v.$message.error("验证码输入错误，请重新输入")
                    return;
                }
                v.$message.success("验证成功！请修改新密码！")
                v.isRight=true;
            },
            submitForm(formName) {
                if(v.responseCode!=v.user.emailCode){
                    v.$message.error("验证码输入错误，请重新输入")
                    return;
                }
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let userDto={password: v.user.password,email:v.user.email};
                        axios.post("/v1/users/changePassword",userDto).then(function (response) {
                            if(response.data.code==1){
                                alert("修改成功！")
                                location.href="/"
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            sendCode() {
                if (!this.user.email) {
                    this.$message.error('邮箱不能为空')
                    return
                }
                if(!this.user.email.includes("@"&&(".com"||".cn"||".net"))){
                    this.$message.error('请输入正确的邮箱格式')
                    return;
                }
                this.isSend = true
                axios.post("/v1/emails/"+v.user.email+"/code").then(function (response) {
                    if(response.data.code==1){
                        v.$message.success("验证码发送成功！")
                        v.responseCode=response.data.data;
                    }
                })
                // 发送验证码的异步操作，这里使用setTimeout模拟异步操作
                setTimeout(() => {
                    this.isSend = false
                    this.isCountingDown = true
                    this.countDownSeconds = 60
                    this.startCountDown()
                }, 1000)
            },
            startCountDown() {
                if (this.countDownSeconds === 0) {
                    this.isCountingDown = false
                    return
                }
                this.countDownSeconds--
                setTimeout(() => {
                    this.startCountDown()
                }, 1000)
            },
        },

    })
</script>
</body>
</html>